﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using MyHospital.DAL;

namespace MyHospital.BLL
{
    public class User
    {
        //建立MyHospital.DAL数据访问层中的MyHospitalDataContext类实例db
        MyHospitalDataContext db = new MyHospitalDataContext();

        /// <summary>
        /// 注册病人用户
        /// </summary>
        /// <param name="patName"></param>
        /// <param name="nickName"></param>
        /// <param name="sex"></param>
        /// <param name="email"></param>
        /// <param name="phone"></param>
        /// <param name="address"></param>
        /// <param name="password"></param>
        /// <param name="idcard"></param>
        public void insertPatient(string patName, string nickName, string sex, string email, string phone, string address, string password, string idcard)//注册
        {
            Patient patient = new Patient()
            {
                patName = patName,
                nickname = nickName,
                sex = sex,
                email = email,
                phone = phone,
                address = address,
                password = password,
                idcard = idcard
            };
            db.Patient.InsertOnSubmit(patient);
            db.SubmitChanges();
        }

        /// <summary>
        /// 注册时，验证手机号是否已存在
        /// </summary>
        /// <param name="phone"></param>
        /// <returns>存在不能注册false；不存在可以注册true</returns>
        public bool isPhoneExit(string phone)
        {
            Patient patient = (from c in db.Patient
                               where c.phone == phone
                               select c).FirstOrDefault();
            if (patient == null)
                return true;
            else return false;
        }

        /// <summary>
        /// 注册时，验证邮箱是否已存在
        /// </summary>
        /// <param name="email"></param>
        /// <returns>存在不能注册false；不存在可以注册true</returns>
        public bool isEmailExit(string email)
        {
            Patient patient = (from c in db.Patient
                               where c.email == email
                               select c).FirstOrDefault();
            if (patient == null)
                return true;
            else return false;
        }

        /// <summary>
        /// 登录时，判断账号是否为电话号码
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="password"></param>
        /// <returns>账号是手机号返回改病人ID</returns>
        public int LoginPhone(string userName, string password)
        {
            Patient result = (from r in db.Patient
                              where r.phone == userName && r.password == password
                              select r).FirstOrDefault();
            if (result != null) return result.patId;
            else return 0;
        }

        /// <summary>
        /// 登录时，判断账号是否为邮箱
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="password"></param>
        /// <returns>账号是邮箱返回病人ID</returns>
        public int LoginEmail(string userName, string password)
        {

            Patient result = (from r in db.Patient
                              where r.email == userName && r.password == password
                              select r).FirstOrDefault();
            if (result != null) return result.patId;
            else return 0;
        }

        /// <summary>
        /// 管理员登录 
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="password"></param>
        /// <returns>密码正确返回改管理员ID</returns>
        public int LonginAdmin(string userName, string password)//管理员登录
        {
            Admin result = (from r in db.Admin
                            where r.account == userName && r.password == password
                            select r).FirstOrDefault();
            if (result != null) return result.adminId;
            else return 0;
        }

        /// <summary>
        /// 重置密码验证电话邮箱是否正确
        /// </summary>
        /// <param name="phone"></param>
        /// <param name="email"></param>
        /// <returns>正确true；错误false</returns>
        public bool GetPwdEmailExit(string phone, string email)
        {
            Patient patient = (from c in db.Patient
                               where c.phone == phone && c.email == email
                               select c).FirstOrDefault();
            if (patient != null)
                return true;
            else return false;
        }

        /// <summary>
        /// 密码重置为abc+电话号码
        /// </summary>
        /// <param name="phone"></param>
        /// <param name="email"></param>
        public void ResetPassword(string phone, string email)
        {
            Patient patient = (from c in db.Patient
                               where c.phone == phone && c.email == email
                               select c).First();
            patient.password = "abc" + phone;
            db.SubmitChanges();
        }

        /// <summary>
        /// 修改密码时，验证病人旧密码是否正确
        /// </summary>
        /// <param name="id"></param>
        /// <param name="password"></param>
        /// <returns>正确true；错误false</returns>
        public bool IsPatientPwdTrue(int id, string password)
        {
            Patient patient = (from c in db.Patient
                               where c.patId == id && c.password == password
                               select c).FirstOrDefault();
            if (patient != null) return true;
            else return false;
        }

        /// <summary>
        /// 修改病人用户密码
        /// </summary>
        /// <param name="id"></param>
        /// <param name="password"></param>
        public void NewPatientPwd(int id, string password)
        {
            Patient patient = (from c in db.Patient
                               where c.patId == id
                               select c).FirstOrDefault();
            patient.password = password;
            db.SubmitChanges();
        }

        /// <summary>
        /// 验证管理员旧密码是否正确
        /// </summary>
        /// <param name="id"></param>
        /// <param name="password"></param>
        /// <returns>正确true；错误false</returns>
        public bool IsAdminPwdTrue(int id, string password)
        {
            Admin admin = (from c in db.Admin
                           where c.adminId == id && c.password == password
                           select c).FirstOrDefault();
            if (admin != null) return true;
            else return false;
        }

        /// <summary>
        /// 修改管理员密码
        /// </summary>
        /// <param name="id"></param>
        /// <param name="password"></param>
        public void NewAdminPwd(int id, string password)
        {
            Admin admin = (from c in db.Admin
                           where c.adminId == id
                           select c).FirstOrDefault();
            admin.password = password;
            db.SubmitChanges();
        }
        /// <summary>
        /// 根据病人id得出病人昵称
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public String findNameOfId(int id)
        {
            var results = (from r in db.Patient
                           where r.patId == id
                           select r).FirstOrDefault().nickname;
            return results;
        }
    }
}
